home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
COMM
/
INTERNET
/
TELNET
/
RLOGIND
/
!RloginD
/
telnet
/
term_c
< prev
next >
Wrap
Text File
|
1993-09-02
|
6KB
|
201 lines
/* > term.c
* (C) 1993 Andrew Brooks
* arb@comp.lancs.ac.uk
* 1.00 Fri Feb 5 11:51:16 GMT 1993 - First incarnation
* 1.10 Wed May 26 12:10:10 BST 1993 - Term_Blocking added, fcntl altered
* 1.11 Wed May 26 12:24:37 BST 1993 - Term_NoCtrlTerm now uses setsid
* 1.12 Wed May 26 21:31:14 BST 1993 - Term_Ready added
* 1.13 Thu Jun 3 22:04:52 BST 1993 - Term_Spool added
* 1.20 Thu Sep 2 11:46:59 BST 1993 - Special version for rxtelnet
*/
static char SCCSid[] = "@(#)term.c 1.20 (C) 1993 arb Terminal library";
/*
* System-independent terminal handling library.
* When compiling this library the appropriate flags must be set:
* TERMIO
* This is typically on System III and System V (and SunOS)
* Look for a termio.h header file and termio manual page
* #define TERMIO
* SGTTY
* This is typically on Version 7 systems
* Look for a sgtty.h header file
* #define SGTTY
* ITTY
* This is typically on BSD systems
* Look for a ioctl.h header file and a tty(4) manual page
* #define ITTY
* struct tchars is also used to contain special characters.
* ioctls TIOCLBI[SC] also used for more flags.
* struct ltchars also used with *more* flags! What a mess.
* If more than one of the above applies (eg.SunOS) then use TERMIO
* If none of the above apply then you are stuck.
*/
/*
* All of these functions take a file descriptor as the first parameter.
* This is an integer identifying the stream to be operated on. If using
* stdio then the FILE * should be converted to a fd using .
*/
#include "term.h"
/* Simple error check */
#ifndef TERMIO
#ifndef SGTTY
#ifndef ITTY
error Must define one of TERMIO, SGTTY or ITTY
#endif
#endif
#endif
#ifndef NULL
#include <stdio.h>
#endif
/* Process groups.
* Related jobs are placed in the same process group. The terminal may
* then be asociated with whichever process group is desired (eg. for job
* control). To set the process group / terminal association one may use
* the ioctl(TIOCSPGRP) or ioctl(TIOCGPGRP) to examine it.
*/
/* Save terminal status.
* Fills the given buffer with the current terminal settings.
* The terminal settings may be restored to the values given in the buffer
* by passing the buffer to the term_restore function.
* Returns 0 for success, -1 for failure.
*/
public int Term_Save(fd, buf)
int fd;
TERM_STRUCT *buf;
{
if (buf==NULL) return(-1);
#ifdef TERMIO
return(ioctl(fd, TERM_GET, buf));
#endif /* TERMIO */
#ifdef SGTTY
/* Should be more in here but I don't know what */
return(ioctl(fd, TERM_GET, buf));
#endif /* SGTTY */
#ifdef ITTY
ioctl(fd, TIOCGETP, &buf->sgttyb);
ioctl(fd, TIOCGETC, &buf->tchars);
ioctl(fd, TIOCGLTC, &buf->ltchars);
ioctl(fd, TIOCLGET, &buf->lmode);
#endif /* ITTY */
}
/* Restore terminal status.
* Restore the terminal to the state given by the contents of the buffer
* (which was filled by term_save). The buffer is not deallocated so
* you must do that yourself.
* Returns 0 for success or -1 for error.
*/
public int Term_Restore(fd, buf)
int fd;
TERM_STRUCT *buf;
{
if (buf==NULL) return(-1);
#ifdef TERMIO
return(ioctl(fd, TERM_SET, buf));
#endif /* TERMIO */
#ifdef SGTTY
/* Should be more in here but I don't know what */
return(ioctl(fd, TERM_SET, buf));
#endif /* SGTTY */
#ifdef ITTY
ioctl(fd, TIOCSETP, &buf->sgttyb);
ioctl(fd, TIOCSETC, &buf->tchars);
ioctl(fd, TIOCSLTC, &buf->ltchars);
ioctl(fd, TIOCLSET, &buf->lmode);
#endif /* ITTY */
}
/* Set raw mode.
* Set the terminal into raw mode.
* This involves setting:
* Punctual input (clear ICANON and set MIN and TIME),
* No character mapping (clear OPOST, INLCR, ICRNL, IUCLC, ISTRIP),
* No flow control (clear IXON),
* No control characters (clear BRKINT, ISIG),
* No echo (clear ECHO).
* Returns 0 for success or -1 for error.
*/
public int Term_Raw(fd)
int fd;
{
TERM_STRUCT settings;
if (Term_Save(fd, &settings)<0) return(-1);
#ifdef TERMIO
/* settings.c_iflag &= ~(INLCR | ICRNL | IUCLC | ISTRIP | IXON | BRKINT);*/
settings.c_iflag &= ~(INLCR | ICRNL | IUCLC | ISTRIP | BRKINT);
settings.c_oflag &= ~OPOST;
settings.c_lflag &= ~(ICANON | ISIG | ECHO);
settings.c_cc[TERM_MIN] = 1; /* read() will block indefinitely */
settings.c_cc[TERM_TIME] = 0;
#endif /* TERMIO */
#if defined ITTY || defined SGTTY
settings.sgttyb.sg_flags |= RAW;
settings.sgttyb.sg_flags &= ~(CRMOD | ECHO | LCASE | CBREAK | TANDEM);
#endif /* ITTY or SGTTY */
Term_Restore(fd, &settings);
return(0);
}
/* Set cooked mode.
* Take the terminal out of raw mode (ie. back to cooked/canonical mode)
* Returns 0 for success or -1 for error.
*/
public int Term_NonRaw(fd)
int fd;
{
TERM_STRUCT settings;
if (Term_Save(fd, &settings)<0) return(-1);
#ifdef TERMIO
/* Don't put IUCLC back unless you want upper case made lower case */
/* settings.c_iflag |= (INLCR | ICRNL | ISTRIP | IXON | BRKINT);*/
settings.c_iflag |= (INLCR | ICRNL | ISTRIP | BRKINT);
settings.c_oflag |= OPOST;
settings.c_lflag |= (ICANON | ISIG | ECHO);
settings.c_cc[TERM_MIN] = 4; /* VEOF */
settings.c_cc[TERM_TIME] = 0; /* VEOL */
#endif /* TERMIO */
#if defined ITTY || defined SGTTY
settings.sgttyb.sg_flags &= ~RAW;
/* Don't add LCASE back in */
/* Do we want cooked or cbreak? Cooked is most likely so no CBREAK */
settings.sgttyb.sg_flags |= (CRMOD | ECHO | TANDEM);
#endif /* ITTY or SGTTY */
Term_Restore(fd, &settings);
return(0);
}
public int Term_NoEcho(fd)
int fd;
{
TERM_STRUCT settings;
if (Term_Save(fd, &settings)<0) return(-1);
#ifdef TERMIO
settings.c_lflag &= ~(ECHO);
#endif /* TERMIO */
#if defined ITTY || defined SGTTY
settings.sgttyb.sg_flags &= ~(ECHO);
#endif /* ITTY or SGTTY */
Term_Restore(fd, &settings);
return(0);
}